<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="roleForm={...noForm};dialogVisible=true;changeVisible=false"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="getList">
              <el-table-column type="index" label="序号" width="50" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="changeOne(scope)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delOne(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParmas.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pageParmas.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog
      :title="cTitle"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rulesForm" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="hSubmit"
        >确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible.sync="showDialogAssign"
    >
      <assign-permission v-if="showDialogAssign" :row-id="userId" @close="showDialogAssign=false" />
    </el-dialog>
  </div>
</template>
<script>
import { addRoles, getRoles, updateRoles, delRoles } from '@/api/settings'
import assignPermission from './assignPermission'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      showDialogAssign: false,
      userId: '',
      roleForm: {
        name: '',
        description: '',
        changeID: ''
      },
      noForm: {
        name: '',
        description: '',
        changeID: ''
      },
      rulesForm: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      currentPage4: 1,
      pageParmas: {
        page: 1,
        pagesize: 2
      },
      dialogVisible: false,
      changeVisible: false,
      total: 2,
      getList: []
    }
  },
  computed: {
    cTitle() {
      return this.changeVisible ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    hAssign(id) {
      this.showDialogAssign = true
      this.userId = id
    },
    delOne(id) {
      delRoles(id)
      if (this.getList.length <= 1) this.pageParmas.page--
      this.loadRoles()
    },
    changeOne(scope) {
      this.roleForm.changeID = scope.row.id
      this.dialogVisible = true
      this.changeVisible = true
      this.roleForm = scope.row
    },
    hSubmit() {
      this.$refs.roleForm.validate((vale) => {
        if (!vale) return
        this.dialogVisible = false
        this.changeVisible ? updateRoles(this.roleForm) : addRoles(this.roleForm)
        this.loadRoles()
      })
    },
    handleSizeChange(val) {
      this.pageParmas.pagesize = val
      this.loadRoles()
    },
    handleCurrentChange(val) {
      this.pageParmas.page = val
      this.loadRoles()
    },
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParmas)
        console.log(res)
        this.getList = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
